//加载方法
$(function (){
    $("#wuliao").hide();
    //模糊查询的方法，生成表格
    $("#tableinfo").bootstrapTable({
        method:"get",
        contentType:"application/json;charset=utf-8",
        url: "/TYMDes/selbg",
        striped: true,
        pageNumber:1,
        pagination:true,
        sidePagination:"server",//设置服务端分页还是客户端分页
        pageSize:3,//页大小
        pageList:[2,3,5],//每页显示的条数
        queryParams:function (params) {
            return {
                pageNum: (params.offset / params.limit) + 1,
                pageSize:params.limit,
                productName:$("#productName").val()
            }
        },
        columns:[
            {title:'设计单编号',field:'designId',align:'center'},
            {title:'产品名称',field:'productName',align:'center'},
            {title:'登记人',field:'register',align:'center'},
            {title:'登记时间',field:'registerTime',align:'center'},
            {title:'备注',field:'moduleDescribe',align:'center'},
            {title:'变更',field:'designModuleId',align:'center',
                formatter:function (value,row,index) {
                    let btn="<button class='btn btn-info' onclick='bg("+value+")'>变更</button>";
                    return btn;
                }
            },
        ]
    })
})

//查询已通过审核的产品
let dFileList=[];
let arr=[];
function getMApply() {
    $.ajax({
        url:"/TYMDfile/selwuliao",
        type:"post",
        dataType:"json",
        success:function (data) {
            dFileList=data;
            let tr = "";
            for (let i = 0; i < data.length; i++) {
                var amountUnit=data[i].amoutUnil;
                if(amountUnit==undefined){
                    amountUnit="";
                }
                tr+="<tr>\n" +
                    "                    <td>"+data[i].productName+"</td>\n" +
                    "                    <td>"+data[i].productId+"</td>\n" +
                    "                    <td>"+amountUnit+"</td>\n" +
                    "                    <td>"+data[i].costPrice+"</td>\n" +
                    "                    <td><button onclick='addInTable("+i+")'>添加</button></td>\n" +
                    "                </tr>";

            }
            //添加到表格
            $("#moduleWatch").html(tr);
        }
    })
}

//点击去审核
let applyId1="";
function bg(applyId){
    $("#table").hide();
    $("#wuliao").show();
    $("#remark").html("");
    applyId1=applyId;
    $.ajax({
        url: "/TYMDes/selbyoneshenhe/"+applyId,
        type:"post",
        contentType: "application/json;charset=utf-8",
        dataType:"json",
        success:function (data){
            $("#designModuleId").val(data.designModuleId);
            $("#register").val(data.register);
            $("#productId").val(data.productId);
            $("#productName").val(data.productName);
            $("#designId").val(data.designId);
            $("#remark").val(data.moduleDescribe);
            let tr ="";
            for (let i = 0; i < data.dList.length; i++) {
                arr.push(i+'');
                var amountUnit;
                if(data.dList[i].amountUnit==undefined){
                    amountUnit="";
                }
                tr +="<tr>\n" +
                    "                <td hidden='hidden'><input value='"+data.dList[i].detailsNumber+"' readonly type=\"text\" name=\"fileId\"></td>\n" +
                    "                <td><input value='"+data.dList[i].productName+"' readonly type=\"text\" name=\"productName\"></td>\n" +
                    "                <td><input value='"+data.dList[i].productId+"' readonly type=\"text\" name=\"productId\"></td>\n" +
                    "                <td><input value='"+data.dList[i].productDescribe+"' readonly type=\"text\" name=\"productDescribe\"></td>\n" +
                    "                <td><input type=\"number\" min='1' class='shuliang' name=\"amount\" value='"+data.dList[i].amount+"' onchange='changeAmount(this,"+data.dList[i].costPrice+")'></td>\n" +
                    "                <td>"+amountUnit+"</td>\n" +
                    "                <td class=\"danjia\">"+data.dList[i].costPrice+"</td>\n" +
                    "                <td class=\"xiaoji\">"+data.dList[i].subtotal+"</td>\n" +
                    "                <td><button type='button' class='btn btn-primary' onclick='delwuliao("+i+")' id='"+i+"'>删除</button></td>\n" +
                    "            </tr>";
            }
            $("#tbody").html(tr);

            //获取数量和小计
            let xiaoji = $(".xiaoji");
            let amounts = $(".shuliang");

            //定义总数量和总金额
            let totalCount=0;
            let totalPrice=0;
            amounts.each(function (i,e){
                totalCount+=parseInt($(e).val());
            })
            xiaoji.each(function (i,e){
                totalPrice+=parseFloat($(e).html());
            })

            $("#totalCount").html(totalCount);
            $("#totalPrice").html(totalPrice);

        }
    })


}

//改变数量
function changeAmount(that,price){
    let amount = $(that).val();
    $(that).parent().next().next().next().html(amount*price);

    //置空总金额和总数量
    $("#totalCount").html("");
    $("#totalPrice").html("");
    $("#btn-save").hide();
}

//点击预览
function preview(){
    if($("#tbody").html().trim() != "") {
        //获取数量和小计
        let xiaoji = $(".xiaoji");
        let amounts = $("input[name='amount']");

        //定义总数量和总金额
        let totalCount=0;
        let totalPrice=0;
        amounts.each(function (i,e){
            totalCount+=parseInt($(e).val());
        })
        xiaoji.each(function (i,e){
            totalPrice+=parseFloat($(e).html());
        })

        $("#totalCount").html(totalCount);
        $("#totalPrice").html(totalPrice);
        $("#btn-save").show();
    } else {
        alert("添加物料后才能预览");
    }
}


function delwuliao(a){
    var number;
    for (var i = 0; i <arr.length ; i++) {
        number = arr.indexOf(a+'');
    }
    arr.splice(number, 1);
    $("#"+a).parent().parent().remove();
    //置空总金额和总数量
    $("#totalCount").html("");
    $("#totalPrice").html("");
    $("#btn-save").hide();
}

//添加产品的方法
function addInTable(index){
    let flag=true;
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] == index){
            flag=false;
        }
    }
    if (flag){
        console.log(index);
        arr.push(index+'');
        let dFileListElement = dFileList[index];
        $("#tbody").append("<tr>\n" +
            "                <td hidden='hidden'><input value='"+dFileListElement.fileId+"' readonly type=\"text\" name=\"fileId\"></td>\n" +
            "                <td><input value='"+dFileListElement.productName+"' readonly type=\"text\" name=\"productName\"></td>\n" +
            "                <td><input value='"+dFileListElement.productId+"' readonly type=\"text\" name=\"productId\"></td>\n" +
            "                <td><input value='"+dFileListElement.productDescribe+"' readonly type=\"text\" name=\"productDescribe\"></td>\n" +
            "                <td><input type=\"number\" min='1' name=\"amount\" value=\"1\" onchange='changeAmount(this,"+dFileListElement.realCostPrice+")'></td>\n" +
            "                <td>"+dFileListElement.amoutUnil+"</td>\n" +
            "                <td class=\"danjia\">"+dFileListElement.realCostPrice+"</td>\n" +
            "                <td class=\"xiaoji\">"+dFileListElement.realCostPrice+"</td>\n" +
            "                <td><button type='button' class='btn btn-primary' onclick='delwuliao("+index+")' id='"+index+"'>删除</button></td>\n" +
            "            </tr>");

        //置空总金额和总数量
        $("#totalCount").html("");
        $("#totalPrice").html("");
        $("#btn-save").hide();
    }


}


//提交生产计划单
function bgbywuliao(){

    var dmodule = {
        designModuleId:$("#designModuleId").val(),
        moduleDescribe : $("#remark").val(),
        costPriceSum : $("#totalPrice").html(),
    }
    //获取四个input框
    let productNames = $("input[name='productName']");
    let productIds = $("input[name='productId']");
    let productDescribes = $("input[name='productDescribe']");
    let amounts = $("input[name='amount']");
    let danjia = $("td[class='danjia']");
    let xiaoji = $("td[class='xiaoji']");
    let fileId = $("input[name='fileId']");

    //定义数组存值
    let mApplyArray=[];
    productIds.each(function (i,e){
        let mApply={
            detailsNumber:fileId[i].value,
            productName:productNames[i].value,
            productId:productIds[i].value,
            productDescribe:productDescribes[i].value,
            amount:amounts[i].value,
            costPrice:danjia[i].innerHTML,
            subtotal:xiaoji[i].innerHTML
        }
        //加入数组
        mApplyArray[i]=mApply;

    })
    dmodule.dList = mApplyArray;
    $.ajax({
        url: "/TYMDes/wuliaobg",
        type: "put",
        contentType:"application/json;charset=utf-8",
        data:JSON.stringify(dmodule),
        success:function (data){
            if(data>0){
                alert("变更成功");
            }else {
                alert("变更失败");
            }
            $("#table").show();
            $("#wuliao").hide();
            $("#tableinfo").bootstrapTable("refresh");
        }
    })
}

